// @ts-nocheck
import { NetPacket } from '@/netBase/NetPacket'
import PKwebsocket from '@/lolo/Ws'
// import H5Websocket from '@/net/Net1'
// import { MessageEvent2 } from '@/net/MessageEvent2'
import { NetMsgType } from '@/netBase/NetMsgType'
import { onMounted, onUnmounted, ref, reactive, computed } from 'vue'
import { lang } from '@/utils/commonVar'
import {
  handleOpenLink,
  formatTime,
  userNameHiding,
} from '@/utils/publicMethods'
import { useRouter } from 'vue-router'
export const redEnvelopeState: any = reactive({
  envelopeInfo: {},
  visible: false,
  isShow: false,
  isOpen: false,
  isOpenInfo: false,
  isGetFunds: false, // 领取红包
  isOpenData: false,
  redBagInfo: {}, // 红包详情
  completeTaskInfo: [], // 完成任务回执
  taskList: [],
  getFundsInfo: {}, // 领取红包数据
  has_next_open: false,
  allRecordList: [], // 领取记录
  myRecordList: [], //我的领取记录
  myRecordInfo: {},
  noticeMsg: '', // 滚动公告
})
// 红包活动hooks
export const useredEnvelopeHooks = () => {
  const router = useRouter()
  const taskNameObj: any = {
    1: { label: '充值', img: '/imgs/activity/factg_icon1.webp' },
    2: { label: '有效投注', img: '/imgs/activity/factg_icon3.webp' },
    3: { label: '期间局数', img: '/imgs/activity/factg_icon3.webp' },
    4: { label: '绑定银行卡', img: '/imgs/activity/factg_icon2.webp' },
    5: { label: '有效好友', img: '/imgs/activity/factg_icon9.webp' },
    6: { label: '有效代理', img: '/imgs/activity/factg_icon9.webp' },
    7: { label: '开启VIP等级', img: '/imgs/activity/factg_icon10.webp' },
    8: { label: '领取VIP等级', img: '/imgs/activity/factg_icon10.webp' },
    9: { label: '分享Facebook', img: '/imgs/activity/factg_icon4.webp' },
    10: { label: '关注Facebook主页', img: '/imgs/activity/factg_icon4.webp' },
    11: { label: '关注Facebook小组', img: '/imgs/activity/factg_icon5.webp' },
    12: { label: '关注Telegram频道', img: '/imgs/activity/factg_icon6.webp' },
    13: { label: '关注Instagram', img: '/imgs/activity/factg_icon7.webp' },
    14: { label: '关注Twitter', img: '/imgs/activity/factg_icon8.webp' },
  }
  const langObj: any = {
    'zh-CN': 2,
    'en-US': 1,
    'vi-VN': 3,
  }
  // 点击红包开启
  const openBag = () => {
    const req = NetPacket.req_open_red_envelope()
    req.language_id = langObj[lang.value] || 2
    PKwebsocket.instance.send(req, true, {
      callbackName: 'msg_notify_open_red_envelope',
      callback: openRedEnvelope,
    })
  }

  // 点击红包开启
  const receiveRed = () => {
    const req = NetPacket.req_receive_red_envelope()
    req.language_id = langObj[lang.value] || 2

    PKwebsocket.instance.send(req, true, {
      callbackName: 'msg_notify_receive_red_envelope',
      callback: getReceiveRedEnvelope,
    })
  }

  // 红包入口
  const envelopeEntry = () => {
    const req1 = NetPacket.req_red_envelope_entry()
    PKwebsocket.instance.send(req1, true, {
      callbackName: 'msg_notify_red_envelope_entry',
      callback: handleRedEnvelope,
    })
  }

  const handleRedEnvelope = (res: any) => {
    redEnvelopeState.envelopeInfo = res
    if (res.is_open==4) {
      console.log(7777777,res)
      receiveRed()
    }
  }

  const closeModal = () => {
    redEnvelopeState.isShow = false
    redEnvelopeState.isOpen = false
    redEnvelopeState.isOpenInfo = false
    envelopeEntry()
  }

  // 开启红包
  const openRedEnvelope = (res: any) => {
    redEnvelopeState.redBagInfo = res
    if (res.is_ok == 1) {
      redEnvelopeState.isOpen = true
      setTimeout(() => {
        redEnvelopeState.isOpenInfo = true
      }, 2000)
    }
  }
  const showTips = ref(false)
  const tipsInfo = ref('')
  // 任务说明
  const showTipsClick = (item: any) => {
    showTips.value = true
    switch (item.task_id) {
      case 1:
        tipsInfo.value = `任务期间充值${Intl.NumberFormat().format(item.task_content)}`
        break
      case 2:
        tipsInfo.value = `任务期间有效投注${Intl.NumberFormat().format(item.task_content)}`
        break
      case 3:
        tipsInfo.value = `任务期间完成${item.task_content}局游戏`
        break

      default:
        tipsInfo.value = item.label
        break
    }
  }

  // 领取红包响应
  const getReceiveRedEnvelope = (res: any) => {
    redEnvelopeState.redBagInfo = res
    redEnvelopeState.isGetFunds = false
    if (res.red_task_list && res.red_task_list.length) {
      redEnvelopeState.taskList = res.red_task_list.map((item: any) => {
        let label
        switch (item.task_id) {
          case 1:
            label = `充值${Intl.NumberFormat().format(item.task_content)}`
            break
          case 2:
            label = `有效投注 ${Intl.NumberFormat().format(item.task_content)}`
            break
          case 3:
            label = `完成${item.task_content}局游戏`
            break
          case 5:
            if (item.task_content == 1) {
              label =
                ' 邀请2位新用户<br><span style="color:#C6C4F5;fontSize:12px">分别开启一级和二级红包</span>'
            } else if (item.task_content == 2) {
              label = '邀请两位用户且新用户开启任意三级红包'
            }
            break
          case 6:
            label = `邀请1位代理、2位玩家<br><span style="color:#C6C4F5;fontSize:12px">开启任意红包</span>`
            break
          case 8:
            label = `VIP等级达到${item.task_content}级`
            break
          case 4:
          case 9:
          case 10:
          case 11:
          case 12:
          case 13:
          case 14:
            label = taskNameObj[item.task_id].label
            break

          default:
            break
        }
        return {
          ...item,
          label: label,
          img: taskNameObj[item.task_id].img,
        }
      })
    }
  }

  //  去完成
  const goFollow = (item: any) => {
    const obj: any = {
      9: 'https://www.facebook.com/PKBET88',
      10: 'https://www.facebook.com/PKBET88',
      11: 'https://www.facebook.com/login/?next=https%3A%2F%2Fwww.facebook.com%2Fgroups%2Fpkbet88',
      12: 'https://t.me/PKBET888',
      13: 'https://www.instagram.com/pkbet888/',
      14: 'https://x.com/PKbet888',
    }
    if (obj[item.task_id]) {
      handleOpenLink(obj[item.task_id])
    }
    if (item.task_id == 1) {
      router.push('/deposit')
    }
    if (item.task_id == 2 || item.task_id == 3) {
      router.push('/')
    }
    if (item.task_id == 5) {
      // 邀请好友
      redEnvelopeState.visible = true
    }
    if (item.task_id == 6) {
      // 邀请代理
      router.push('/agencyCooperation')
    }
    if (item.task_id == 7 || item.task_id == 8) {
      // 邀请代理
      router.push('/vip')
    }
    if (item.task_id == 4) {
      // 绑定银行卡
      router.push('/paymentManagement/bank')
    }
  }

  // 联系客服
  const customerService = () => {
    const query = NetPacket.req_contact_customer_service()

    PKwebsocket.instance.send(query, true, {
      callbackName: 'msg_notify_receive_red_envelope',
      callback: () => {},
    })
    handleOpenLink('https://t.me/PKBET01')
  }
  // 立即领取
  const getFunds = () => {
    if (redEnvelopeState.isGetFunds) {
      redEnvelopeState.showToast = true
      redEnvelopeState.toast_value = '>全部红包已领取'

      return
    }
    const query = NetPacket.req_complete_task_and_get_red_envelope()
    PKwebsocket.instance.send(query, true, {
      callbackName: 'msg_notify_complete_task_and_get_red_envelope',
      callback: noticeGetFunds,
    })
  }
  // 立即领取回执
  const noticeGetFunds = (res: any) => {
    redEnvelopeState.getFundsInfo = res
    if (res.is_ok == 3) {
      redEnvelopeState.showToast = true
      redEnvelopeState.toast_value = '未达到领取任务，请完成领取任务后再来领取'
    }
    // 红包领取
    if (res.is_ok == 1) {
      redEnvelopeState.isShow = true
      redEnvelopeState.isOpen = true
      redEnvelopeState.has_next_open = res.is_next_open == 1
      setTimeout(() => {
        redEnvelopeState.isOpenInfo = true
        redEnvelopeState.isGetFunds = true
      }, 2000)
    }
  }

  const setName = computed(() => {
    if (redEnvelopeState.isOpenInfo && !redEnvelopeState.isGetFunds) {
      return '去领取'
    }
    if (redEnvelopeState.isGetFunds && redEnvelopeState.has_next_open) {
      return '确定'
    }
    if (redEnvelopeState.isGetFunds && !redEnvelopeState.has_next_open) {
      return '返回'
    }
  })
  // 获奖记录
  const awardRecordClick = () => {
    const query = NetPacket.req_red_envelope_activity_receive_record()
    PKwebsocket.instance.send(query, true, {
      callbackName: 'msg_notify_red_envelope_activity_receive_record',
      callback: awardRecordResult,
    })
  }
  // 我的記錄
  const awardMyRecordClick = () => {
    const query = NetPacket.req_red_envelope_my_receive_record()
    PKwebsocket.instance.send(query, true, {
      callbackName: 'msg_notify_red_envelope_my_receive_record',
      callback: notifyMyRecordClick,
    })
  }

  const awardRecordResult = (res: any) => {
    redEnvelopeState.allRecordList = res.red_envelope_receive_record_list.map(
      (item: any) => {
        redEnvelopeState.noticeMsg =
          redEnvelopeState.noticeMsg +
          ` <span style="margin-right:20px">玩家${userNameHiding(
            item.nickname,
            3
          )}领取了红包</span>`
        return {
          ...item,
          nickname: userNameHiding(item.nickname, 3),
          receive_time: formatTime(item.receive_time),
          money: Intl.NumberFormat().format(item.money),
        }
      }
    )
  }
  // 我的記錄

  const notifyMyRecordClick = (res: any) => {
    redEnvelopeState.myRecordInfo = res
    if (res.red_envelope_my_receive_record_list.length) {
      const levelObj: any = {
        1: '一重礼',
        2: '二重礼',
        3: '三重礼',
        4: '四重礼',
        5: '五重礼',
      }
      redEnvelopeState.myRecordList = res.red_envelope_my_receive_record_list.map(
        (item: any, index: any) => {
          return {
            ...item,
            level: levelObj[item.level],
            key: index + 1,
          }
        }
      )
    }
    console.log(555555, redEnvelopeState.myRecordList)
  }

  onMounted(() => {})
  onUnmounted(() => {})

  return {
    openBag,
    closeModal,
    receiveRed,
    NetMsgType,
    getReceiveRedEnvelope,
    openRedEnvelope,
    goFollow,
    customerService,
    showTipsClick,
    showTips,
    tipsInfo,
    getFunds,
    setName,
    awardRecordClick,
    awardMyRecordClick,
    envelopeEntry,
  }
}
